using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookInfoCS
{

    [ToolboxData("<{0}:SampleDataBound runat=server></{0}:SampleDataBound>")]
    class SampleDataBound : System.Web.UI.WebControls.DataBoundControl
    {
        public string DataTextField
        {
            get
            {
                string strDTF;
                strDTF = ViewState["DTF"].ToString();
                if (strDTF == null)
                {
                    return String.Empty;
                }
                else
                {
                    return strDTF;
                }
            }
            set
            {
                if (this.Initialized == true)
                {
                    this.OnDataPropertyChanged();
                }
                ViewState["DTF"] = value;
            }
        }

        protected override void PerformSelect()
        {
            System.Web.UI.DataSourceSelectArguments dsa;
            System.Web.UI.DataSourceView dsv;

            if (this.IsBoundUsingDataSourceID == false)
            {
                this.OnDataBinding(EventArgs.Empty);
            }
            dsa = new System.Web.UI.DataSourceSelectArguments();
            dsa.MaximumRows = 10;

            dsv = this.GetData();
            if (dsv.CanRetrieveTotalRowCount == true)
            {
                dsa.RetrieveTotalRowCount = true;
            }
            dsv.Select(dsa, this.HandleRetrievedData);
            RequiresDataBinding = false;
            MarkAsDataBound();
            OnDataBound(EventArgs.Empty);
        }
        private void HandleRetrievedData(System.Collections.IEnumerable data)
        {
            PerformDataBinding(data);
        }

        public void InsertData()
        {
            StateBag InsertData = new StateBag();
            System.Web.UI.DataSourceView dsv;

            dsv = this.GetData();
            if (dsv.CanInsert == true)
            {
                InsertData.Add("CustId", ((TextBox)this.FindControl("tb1")).Text);
                InsertData.Add("CustName", ((TextBox)this.FindControl("tb2")).Text);
                dsv.Insert(InsertData, new DataSourceViewOperationCallback(HandleInsert));
            }
        }

        public bool HandleInsert(int affectedRecords, Exception ex)
        {
            if (ex != null)
            {
                throw ex;
            }
            else
            {
                return true;
            }
        }

        protected override void PerformDataBinding(System.Collections.IEnumerable data)
        {
            System.Web.UI.WebControls.TextBox tb;
            int ctr;
            string strFieldName;

            base.PerformDataBinding(data);

            if (data != null)
            {
                if (DataTextField.CompareTo("") > 0)
                {
                    strFieldName = DataTextField;
                    ctr = 0;
                    foreach (object objData in data)
                    {
                        tb = new System.Web.UI.WebControls.TextBox();
                        ctr = ctr + 1;
                        tb.ID = "tb" + ctr;
                        tb.Text = System.Web.UI.DataBinder.GetPropertyValue(objData,
                                                  strFieldName, null);
                        this.Controls.Add(tb);
                    }
                }
            }
        }
    }
}
